package com.jbookshelf.dal.providers;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.exception.ConstraintViolationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.jbookshelf.dal.entities.Languages;

@Repository
@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
public class LanguagesDataProviderImpl implements LanguagesDataProvider {

	@Autowired
	SessionFactory sessionFactory;

	@Override
	@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
	public List<Languages> getLanguages() {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Languages");
		return query.list();
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public void insertLanguage(Languages lang) {
		Session s = sessionFactory.getCurrentSession();
		s.save(lang);

	}
}
